#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "defines.h"
void path_abs(char* path_abs, char* path_pre)
{
	strcpy(path_abs, path_pre);
}

void dopath(char* pathname, HOOK_FILE myfunc)
{
	struct stat statbuf;
	struct dirent *dirp;
	DIR *dp;
 	char *ptr;

	printf ("cur pathname: %s\n", pathname);
	fflush(stdout);

	/* stat error */
	if (lstat(pathname, &statbuf)<0)
	{
		printf ("stat error:[%s]\n", pathname);
		return;
	}

	/* not a directory */
	if (S_ISDIR(statbuf.st_mode)==0)
	{
		myfunc(pathname, &statbuf, FTW_F);
		return;
	}

	/* Directory */
	myfunc(pathname, &statbuf, FTW_D);
	
	ptr=pathname+strlen(pathname);
	*ptr++='/';
	*ptr=0;
	
	if ( (dp=opendir(pathname))==NULL )
	{
		myfunc(pathname, &statbuf, FTW_DNR);
		return;
	}

	while ( (dirp=readdir(dp))!=NULL )
	{
		if ( (strcmp(dirp->d_name, ".") == 0) || (strcmp(dirp->d_name, "..")==0) )
		{
			continue;
		}
		strcpy(ptr, dirp->d_name);
		dopath(pathname, myfunc);
	}

	ptr[-1]=0;
	
	if (closedir(dp)<0)
	{
		printf ("can't close directory: %s\n", pathname);
	}

	return;
}
